from PyQt5.QtCore import QUrl
from PyQt5.QtMultimedia import QMediaContent
from PyQt5.QtWidgets import QWidget, QPushButton, QMessageBox


class Play(QWidget):
    """管理播放音乐的类"""
    def __init__(self,MP3Player):
        super().__init__()
        # 基本控件
        self.prevBtn = QPushButton(self)
        self.nextBtn = QPushButton(self)
        self.is_switching = False
        # 取自MP3Player中的控件
        self.ms = MP3Player.ms
        self.player = MP3Player.player
        self.slider = self.ms.slider
        self.playBtn = MP3Player.playBtn
        self.file = MP3Player.file
        self.lyrics_window = MP3Player.lyrics_window
        self.lyrics = MP3Player.lyrics
        # 初始化
        self.nextBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/next.png)}")
        self.nextBtn.setFixedSize(32, 32)
        self.prevBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/prev.png)}")
        self.prevBtn.setFixedSize(32, 32)
        # SignalSlot
        self.prevBtn.clicked.connect(self.prevMusic)
        self.nextBtn.clicked.connect(self.nextMusic)
        self.file.musicList.itemDoubleClicked.connect(self.doubleClicked)

    def Tips(self, message):
        """提示"""
        QMessageBox.about(self, "提示", message)

    def setCurPlaying(self):
        """设置当前播放的音乐"""
        self.file.cur_playing_song = self.file.songs_list[self.file.musicList.currentRow()][-1]
        self.player.setMedia(QMediaContent(QUrl(self.file.cur_playing_song)))
        # 获取歌词
        self.cur_lyrics = str(self.file.cur_playing_song).replace(str(self.file.cur_playing_song).split('.')[-1], 'txt')
        self.lyrics_content = self.lyrics.get_lyrics(self.cur_lyrics)
        self.lyrics_window.set_text(self.lyrics_content)

    def doubleClicked(self):
        """双击歌曲名称播放音乐"""
        self.slider.setValue(0)
        self.is_switching = True
        self.setCurPlaying()
        self.playMusic()
        self.is_switching = False

    def playMusic(self):
        """播放/暂停播放"""
        if self.file.musicList.count() == 0:
            self.Tips('当前路径内无可播放的音乐文件')
            return
        if not self.player.isAudioAvailable():
            self.setCurPlaying()
        if self.file.is_pause or self.is_switching:

            self.player.play()
            self.file.is_pause = False
            self.playBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/pause.png)}")
        elif (not self.file.is_pause) and (not self.is_switching):
            self.player.pause()
            self.file.is_pause = True
            self.playBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/play.png)}")

    def prevMusic(self):
        """上一曲"""
        self.slider.setValue(0)
        if self.file.musicList.count() == 0:
            self.Tips('当前路径内无可播放的音乐文件')
            return
        pre_row = self.file.musicList.currentRow() - 1 if self.file.musicList.currentRow() != 0 else self.file.musicList.count() - 1
        self.file.musicList.setCurrentRow(pre_row)
        self.is_switching = True
        self.setCurPlaying()
        self.playMusic()
        self.is_switching = False

    def nextMusic(self):
        """下一曲"""
        self.slider.setValue(0)
        if self.file.musicList.count() == 0:
            self.Tips('当前路径内无可播放的音乐文件')
            return
        next_row = self.file.musicList.currentRow() + 1 if self.file.musicList.currentRow() != self.file.musicList.count() - 1 else 0
        self.file.musicList.setCurrentRow(next_row)
        self.is_switching = True
        self.setCurPlaying()
        self.playMusic()
        self.is_switching = False